package com.cn.onetrip.activity;

import com.cn.onetrip.objects.RouteObj;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.view.AudioPlayLayout;
import com.cn.onetrip.view.CustomMapView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

public class CustomMapActivity extends Activity {

	private ImageView returnBtn;
	private ImageView routeShowBtn;
	private ImageView routeHideBtn;
	private ImageView mapLocationBtn;
	private CustomMapView mapView;

	private RelativeLayout layoutProgress;
	private boolean isquit = false;
	private boolean isMapLoad = false;

	private RouteObj routeObj = new RouteObj();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		if (SysApplication.routesList.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		setContentView(R.layout.activity_custom_map);

		returnBtn = (ImageView) findViewById(R.id.map_return);
		routeShowBtn = (ImageView) findViewById(R.id.map_route_show);
		routeHideBtn = (ImageView) findViewById(R.id.map_route_hide);
		mapLocationBtn = (ImageView) findViewById(R.id.map_location);
		mapView = (CustomMapView) findViewById(R.id.map_view);
		layoutProgress = (RelativeLayout) findViewById(R.id.layout_progress);

		buttonClickListener();

		routeObj = SysApplication.routesList.get(getIntent().getIntExtra(
				"route_index", 0));

		setProgressShow(true);

		ViewTreeObserver vedioVTO = mapView.getViewTreeObserver();
		vedioVTO.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {

				if (isMapLoad) {
					mapView.getViewTreeObserver().removeGlobalOnLayoutListener(
							this);
					return;
				}

				// get screen width and height
				DisplayMetrics metrics = new DisplayMetrics();
				getWindowManager().getDefaultDisplay().getMetrics(metrics);
				int screenWidth = metrics.widthPixels;
				int screenHeight = metrics.heightPixels;

				mapView.setData(routeObj, mapHandler, true, screenWidth,
						screenHeight);
				isMapLoad = true;
			}
		});
	}

	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);

		mapView.configurationChanged();
		mapView.resetRouteLine();
	}

	public Handler mapHandler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {

			if (msg.what == CustomMapView.MAP_LOAD_FAIL) {
				loadMapError();
			} else if (msg.what == CustomMapView.MAP_LOAD_SUCCESS) {
				mapView.setVisibility(View.VISIBLE);
			} else if (msg.what == CustomMapView.MAP_QUALITY_LOW) {
				Toast.makeText(getApplicationContext(),
						getResources().getString(R.string.map_low_quality_msg),
						Toast.LENGTH_LONG).show();
			}

			setProgressShow(false);

		};
	};

	private void loadMapError() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle(R.string.map_load_error_title);
		builder.setMessage(R.string.map_load_error_msg);
		builder.setNegativeButton(R.string.confirm,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						returnClick();
					}
				});
		builder.create();
		builder.show();
	}

	private void buttonClickListener() {
		returnBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				returnClick();
			}
		});

		routeShowBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mapView.isShowRoute())
					return;

				mapView.setShowRoute(true);
				routeShowBtn.setBackgroundResource(R.drawable.route_show_down);
				routeHideBtn.setBackgroundResource(R.drawable.route_hide_up);

				setProgressShow(true);
				mapView.loadMap();
			}
		});

		routeHideBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!mapView.isShowRoute())
					return;

				mapView.setShowRoute(false);
				routeShowBtn.setBackgroundResource(R.drawable.route_show_up);
				routeHideBtn.setBackgroundResource(R.drawable.route_hide_down);

				setProgressShow(true);
				mapView.hidePopup();
				mapView.showAllPops();
				mapView.loadMap();

			}
		});

		mapLocationBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.putExtra("route_index", routeObj.index);
				intent.setClass(CustomMapActivity.this,
						ScenicMapLocationActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_right_translate,
						R.anim.pull_left_translate);
			}
		});

	}

	private void setProgressShow(boolean isShow) {
		layoutProgress.setVisibility(isShow ? View.VISIBLE : View.GONE);
		returnBtn.setClickable(!isShow);
		routeShowBtn.setClickable(!isShow);
		mapLocationBtn.setClickable(!isShow);
	}

	@Override
	protected void onPause() {
		// mapView.hidePopup();
		// mapView.showAllPops();
		mapView.setScreenLock();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			returnClick();
		}
		return super.onKeyDown(keyCode, event);
	}

	private void returnClick() {
		if (mapView != null) {
			mapView.clearAudio();
		}
		finish();
		overridePendingTransition(R.anim.push_left_translate,
				R.anim.pull_right_translate);
		System.gc();
	}
}
